﻿using Dapper;
using System.Collections.Generic;
using System.Linq;
using EasyIot.Model;

namespace EasyIot.Service
{
    public class ItemsDetailService : BaseService<ItemsDetailModel>
    {
        public static dynamic GetListByFilter(ItemsDetailModel filter, PageInfo pageInfo)
        {
            pageInfo.prefix = "a.";
            string _where = " itemsdetail a INNER JOIN items b ON a.ItemId=b.Id";
            if (!string.IsNullOrEmpty(filter.ItemCode))
            {
                _where += string.Format(" and {0}ItemCode like concat('%',@ItemCode,'%')", pageInfo.prefix);
            }
            if (!string.IsNullOrEmpty(filter.ItemName))
            {
                _where += string.Format(" and {0}ItemName like concat('%',@ItemName,'%')", pageInfo.prefix);
            }
            if (filter.ItemId != 0)
            {
                _where += string.Format(" and {0}ItemId=@ItemId", pageInfo.prefix);
            }
            pageInfo.returnFields = string.Format("{0}Id,{0}ItemCode,{0}ItemName,{0}SortCode,{0}SystemSign,b.FullName as 'ItemType',{0}CreateTime", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, _where);
        } 

        public static bool DeleteContactById(string deleteWhere, DynamicParameters dpar)
        {
            return DeleteByWhere(deleteWhere, dpar);
        }

        public static bool IsExistCode(string itemcode)
        {
            string where = "where ItemCode=@ItemCode"; 
            return GetByWhere(where, new { ItemCode = itemcode }).Count() > 0 ? true : false;
        }

        public static bool DeleteByIds(string[] idsArray)
        {
            return DeleteByWhere("where Id in @Ids and SystemSign=0", new { Ids = idsArray });
        }
    }
}
